package kr.dhe.gugudan.tu01_gugudan_twonine;

import java.util.ArrayList;
import kr.co.nowgugudan.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.SurfaceView;
import android.widget.ImageView;

public class TwoGugudan extends Activity {
	Vibrator mVib;
	/*ImageView mAnimTarget1;
	Animation anim1 = null;
	ImageView mAnimTarget2;
	Animation anim2 = null;
	ImageView mAnimTarget3;
	Animation anim3 = null;
	ImageView mAnimTarget4;
	Animation anim4 = null;
	ImageView mAnimTarget5;
	Animation anim5 = null;
	ImageView mAnimTarget6;
	Animation anim6 = null;
	ImageView mAnimTarget7;
	Animation anim7 = null;
	ImageView mAnimTarget8;
	Animation anim8 = null;
	ImageView mAnimTarget9;
	Animation anim9 = null;*/
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.tu02_gugudan_twonine);
		setContentView(new SurView(this));
		mVib=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		
		/*mAnimTarget1 = (ImageView)findViewById(R.id.geView111);
		anim1 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
				   Animation.RELATIVE_TO_SELF, 0.5f);
		anim1.setDuration(1000);
		mAnimTarget1.startAnimation(anim1);
			
		mAnimTarget2 = (ImageView)findViewById(R.id.geView22);
		anim2 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
						   Animation.RELATIVE_TO_SELF, 0.5f);
		anim2.setDuration(2000);
		mAnimTarget2.startAnimation(anim2);
				
		mAnimTarget3 = (ImageView)findViewById(R.id.geView33);
		anim3 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
						   Animation.RELATIVE_TO_SELF, 0.5f);
		anim3.setDuration(3000);
		mAnimTarget3.startAnimation(anim3);
					
		mAnimTarget4 = (ImageView)findViewById(R.id.geView44);
		anim4 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
							   Animation.RELATIVE_TO_SELF, 0.5f);
		anim4.setDuration(4000);
		mAnimTarget4.startAnimation(anim4);
						
		mAnimTarget5 = (ImageView)findViewById(R.id.geView55);
		anim5 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
								   Animation.RELATIVE_TO_SELF, 0.5f);
		anim5.setDuration(5000);
		mAnimTarget5.startAnimation(anim5);
							
		mAnimTarget6 = (ImageView)findViewById(R.id.geView66);
		anim6 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
									   Animation.RELATIVE_TO_SELF, 0.5f);
		anim6.setDuration(6000);
		mAnimTarget6.startAnimation(anim6);
								
		mAnimTarget7 = (ImageView)findViewById(R.id.geView77);
		anim7 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
				   Animation.RELATIVE_TO_SELF, 0.5f);
		anim7.setDuration(7000);
		mAnimTarget7.startAnimation(anim7);
		
		mAnimTarget8 = (ImageView)findViewById(R.id.geView88);
		anim8 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
				   Animation.RELATIVE_TO_SELF, 0.5f);
		anim8.setDuration(8000);
		mAnimTarget7.startAnimation(anim8);
		
		mAnimTarget9 = (ImageView)findViewById(R.id.geView99);
		anim9 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
				   Animation.RELATIVE_TO_SELF, 0.5f);
		anim9.setDuration(9000);
		mAnimTarget7.startAnimation(anim9);*/
	}

	public class SurView extends SurfaceView implements Callback {
		Bitmap mBack;
		ArrayList<Ball> arBall;
		final static int DELAY = 50;
		SurfaceHolder mHolder;
		DrawThread mThread;
		MediaPlayer mPlayer;
		
		
		public SurView(Context context) {
			super(context);
			mBack = BitmapFactory.decodeResource(context.getResources(),R.drawable.two2);
			arBall = new ArrayList<Ball>();
			mHolder = getHolder();
			mHolder.addCallback(this);
		}
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			mThread = new DrawThread(mHolder, arBall);
			mThread.start();
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			if (mThread != null){
				mThread.SizeChange(width, height);
			}
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			mThread.bExit = true;
			for(;;){
				try{
					mThread.join();	//mThread가 종료할때까지 대기
					break;
				}catch(Exception e){}
			}
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			
			if (event.getAction() == MotionEvent.ACTION_DOWN){
				synchronized (mHolder) {
					Ball B = new Ball((int)event.getX(), (int)event.getY());
					mVib.vibrate(500);
					arBall.add(B);
					
				}
				return true;
			}
			return false;
		}
		/*public void mOnClick(View v){
			
			switch(v.getId()){
			case R.id.end_btn01:
				player=MediaPlayer.create(this, R.raw.dingdong);
				player.start();
				break;
			}
		}*/
		public class DrawThread extends Thread {
			boolean bExit;
			int mWidth;
			int mHeight;
			SurfaceHolder mHolder;
			ArrayList<Ball> arBall;
			
			public DrawThread(SurfaceHolder mHolder, ArrayList<Ball> arBall) {
				super();
				this.mHolder = mHolder;
				this.arBall = arBall;
			}
			
			public void SizeChange(int width, int height){
				this.mWidth = width;
				this.mHeight = height;
			}
			
			public void run(){
				Canvas canvas;
				Ball B;
				
				while(bExit==false){
					for(int i=0; i<arBall.size(); i++){
						B = arBall.get(i);
						B.Move(mWidth, mHeight);
						if (B.count>4){
							arBall.remove(i);
							i--;
						}
					}//end of for
					
					synchronized (mHolder) {
						canvas = mHolder.lockCanvas();
						if (canvas==null)break;
						canvas.drawColor(Color.LTGRAY);
						Rect dst = new Rect(0, 0, getWidth(), getHeight());
						canvas.drawBitmap(mBack, null, dst, null);
						for(int i=0; i<arBall.size(); i++){
							arBall.get(i).Draw(canvas);
							if (bExit) break;
						}
						mHolder.unlockCanvasAndPost(canvas);//메모리에 있는 그림을 Canvas에 그림
					}//end of synchronized
					
					try{
						sleep(50);
					}catch(Exception e){;}
				
				}//end of while
			}
		
		}
	}
}